package megajournal.view;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.JTextField;
import javax.swing.SpinnerNumberModel;
import megajournal.domain.Group;
import megajournal.view.ocDialog;

/**
 *
 * @author cska
 * 
 */
public class GroupDlg extends JDialog implements ocDialog {

    protected GridBagConstraints c;
    protected JTextField spec;
    protected JTextField letter;
    protected JSpinner course;
    protected JSpinner number;
    protected JLabel l1, l2, l3, l4;
    protected JPanel buttonPanel;
    protected JButton ok;
    protected JButton cancel;
    protected Group group;
    public boolean fl;

    public GroupDlg(JFrame owner, boolean modal, String title, Group group) {
        super(owner, modal);
        setTitle(title);
        setLayout(new GridBagLayout());

        if (group != null) {
            this.group = group;
        } else {
            this.group = new Group("", "", 1, 1);
        }

        c = new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER,
                GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0);

        c.weightx = 0.1;
        l1 = new JLabel("Специальность:", JLabel.RIGHT);
        add(l1, c);
        c.gridy = 1;
        l2 = new JLabel("Литера:", JLabel.RIGHT);
        add(l2, c);
        c.gridy = 2;
        l3 = new JLabel("Курс:", JLabel.RIGHT);
        add(l3, c);
        c.gridy = 3;
        l4 = new JLabel("Номер:", JLabel.RIGHT);
        add(l4, c);

        c.weightx = 0.9;
        c.gridx = 1;
        c.gridy = 0;
        spec = new JTextField(this.group.getSpeciality(), 5);
        add(spec, c);
        c.gridy = 1;
        letter = new JTextField(this.group.getLetter(), 5);
        add(letter, c);
        c.gridy = 2;
        course = new JSpinner(new SpinnerNumberModel(this.group.getCourse(), 1, 6, 1));
        add(course, c);
        c.gridy = 3;
        number = new JSpinner(new SpinnerNumberModel(this.group.getNumber(), 1, 9, 1));
        add(number, c);

        buttonPanel = new JPanel(new GridBagLayout(), true);
        c.gridx = 0;
        c.gridy = 0;
        c.weightx = 0.0;
        c.fill = GridBagConstraints.CENTER;
        c.anchor = GridBagConstraints.EAST;
        c.insets = new Insets(0, 0, 0, 5);
        ok = new JButton("Сохранить");
        ok.addActionListener(new okAdapter(this));
        buttonPanel.add(ok, c);
        c.gridx = 1;
        c.insets = new Insets(0, 5, 0, 0);
        cancel = new JButton("Отмена");
        cancel.addActionListener(new cancelAdapter(this));
        buttonPanel.add(cancel, c);
        c.gridx = 0;
        c.gridy = 4;
        c.gridwidth = 2;
        c.insets = new Insets(5, 5, 5, 5);
        add(buttonPanel, c);

        setSize(320, 200);
        Dimension screen = getToolkit().getScreenSize().getSize();
        setLocation((screen.width - 320) / 2, (screen.height - 200) / 2);
        setResizable(false);
        setVisible(true);
        setDefaultCloseOperation(HIDE_ON_CLOSE);
    }

    public void setGroup(Group group) {
        this.group = group;
    }

    public Group getGroup() {
        return group;
    }

    public void ok() {
        group.setSpeciality(spec.getText());
        group.setLetter(letter.getText());
        group.setCourse((Integer) course.getModel().getValue());
        group.setNumber((Integer) number.getModel().getValue());
        fl = true;
        setVisible(false);
    }

    public void cancel() {
        fl = false;
        setVisible(false);
    }

}
